package com.zcaxer.roomwordsample.data

import androidx.room.Entity
import androidx.room.Ignore
import androidx.room.PrimaryKey
import com.google.gson.annotations.Expose


@Entity(tableName = "Building_table")
data class Building(
    @PrimaryKey @Expose var buildingID: Int,
    @Expose var buildingName: String,
    @Expose var foodIncrease: Int,
    @Expose var maxWorkersA: Float,
    @Expose var maxWorkersB: Int,
    var level: Int = 1, var reserve: Int = 0, var workers: Int = 0,
    var maxWorkers: Int = 0, var capacity: Int = 0, var upNeed: Int = 1
) {
    @Expose
    @Ignore
    var upgradeInfo: List<Map<String, Int>> = listOf(mapOf("endLevel" to -1, "a" to 0, "b" to 0, "c" to 0))


    companion object {

        fun calculate(type: Int, infoList: List<Map<String, Int>>, l: Int): Int {
            val level=if(l>0) {l}else{1}
            var x = 0
            infoList.let {
                val iterator = it.iterator()
                while (iterator.hasNext()) {
                    val info = iterator.next()
                    if (type == info.getOrElse("type", { -1 })) {
                        if (level < info.getOrElse("endLevel", { 400 }))
                            x = info.getOrElse("start", { 0 })
                        +level * info.getOrElse("delta", { 0 })
                        +level * (l - 1) * info.getOrElse("delta2", { 0 }) / 2
                        break
                    }
                }
            }
            return x
        }
    }

    fun initBuilding(): Building {
        level=if(level>0) {level}else{1}
        maxWorkers = (maxWorkersA * level + maxWorkersB).toInt()
        capacity = calculate(0, upgradeInfo, level)
        upNeed = calculate(1, upgradeInfo, level)

        return this
    }

}




